package org.abacus.http;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public abstract class HttpRequest<T, D> implements Request<T, D> {
	private final URL url;

	private String charsetEncoding = "UTF-8";

	private String contentType = "application/x-www-form-urlencoded";

	private String contentLength = "0";

	private String requestMethod = "POST";

	private String cookie = "";

	protected HttpURLConnection conn;

	public HttpRequest(String url) throws HttpException {
		super();
		try {
			this.url = new URL(url);
		} catch (MalformedURLException e) {
			throw new HttpException(e.getMessage(), e, "MalformedURLException");
		}
		try {
			conn = (HttpURLConnection) this.url.openConnection();
		} catch (IOException e) {
			throw new HttpException(e.getMessage(), e, "IOException");
		}
	}

	public void setRequestProperty(String key, String value) {
		conn.setRequestProperty(key, value);
	}

	public URL getUrl() {
		return url;
	}

	public String getCharsetEncoding() {
		return charsetEncoding;
	}

	public void setCharsetEncoding(String charsetEncoding) {
		this.charsetEncoding = charsetEncoding;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentLength() {
		return contentLength;
	}

	public void setContentLength(String contentLength) {
		this.contentLength = contentLength;
	}

	public String getRequestMethod() {
		return requestMethod;
	}

	public void setRequestMethod(String requestMethod) {
		this.requestMethod = requestMethod;
	}

	public String getCookie() {
		return cookie;
	}

	public void setCookie(String cookie) {
		this.cookie = cookie;
	}
}
